﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ExcelFileQueryTool
{
    public sealed class EyesCareExcelWorkLogGrid : ListBox
    {
        public EyesCareExcelWorkLogGrid()
        {
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (this.DesignMode)
                return;

            var rowId = this.Name + "_grid_id_" + e.Index;
            if (this.Controls.IndexOfKey(rowId) == -1)
            {
                var row = new EyesCareExcelWorkLogRow((ExcelWorkLogItem)this.Items[e.Index]);
                row.Name = rowId;

                if (e.Index % 2 == 0)
                    row.BackColor = Color.Lavender;
                else
                    row.BackColor = Color.LightYellow;

                this.Controls.Add(row);
                row.Location = e.Bounds.Location;
                row.Width = this.Width;
                row.Height = 90;
            }
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            e.ItemHeight = 90;
            base.OnMeasureItem(e);
        }

        public sealed class EyesCareExcelWorkLogRow : UserControl
        {
            #region Controls Definition

            private readonly Label _typeLabel = new Label();
            private readonly Label _dateLabel = new Label();
            private readonly Label _totalLabel = new Label();
            private readonly Label _productLabel = new Label();
            private readonly Label _unitLabel = new Label();
            private readonly Label _amountLabel = new Label();
            private readonly Label _priceLabel = new Label();
            private readonly Label _loadingFeeLabel = new Label();
            private readonly Label _paidAmountLabel = new Label();
            private readonly Label _payeeLabel = new Label();
            private readonly Label _clientLabel = new Label();
            private readonly Label _transferLabel = new Label();
            private readonly Label _noteLabel = new Label();

            private readonly Label _typeText = new Label();
            private readonly Label _dateText = new Label();
            private readonly Label _totalText = new Label();
            private readonly Label _productText = new Label();
            private readonly Label _unitText = new Label();
            private readonly Label _amountText = new Label();
            private readonly Label _priceText = new Label();
            private readonly Label _loadingFeeText = new Label();
            private readonly Label _paidAmountText = new Label();
            private readonly Label _payeeText = new Label();
            private readonly Label _clientText = new Label();
            private readonly Label _transferText = new Label();
            private readonly Label _noteText = new Label();

            #endregion

            private static readonly Color ValueTextColor = Color.Green;
            private static readonly Font ValueTextFont = new Font(SystemFonts.MessageBoxFont.FontFamily, 12, FontStyle.Bold);

            public EyesCareExcelWorkLogRow(ExcelWorkLogItem item)
            {
                InitializeControls();

                InitializeControlValue(item);
            }

            private void InitializeControlValue(ExcelWorkLogItem item)
            {
                if (item == null)
                {
                    _typeText.Text = "";
                    _dateText.Text = "";
                    _totalText.Text = "";
                    _productText.Text = "";
                    _unitText.Text = "";
                    _amountText.Text = "";
                    _priceText.Text = "";
                    _loadingFeeText.Text = "";
                    _paidAmountText.Text = "";
                    _payeeText.Text = "---";
                    _clientText.Text = "";
                    _transferText.Text = "";
                    _noteText.Text = "";
                }
                else
                {
                    _typeText.Text = item.类型;
                    _dateText.Text = item.日期.ToString("yyyy-MM-dd");
                    _totalText.Text = item.货款合计.ToString("0.00##");
                    _productText.Text = item.货物;
                    _unitText.Text = item.单位;
                    _amountText.Text = item.数量.ToString("0.00##");
                    _priceText.Text = item.单价.ToString("0.00##");
                    _loadingFeeText.Text = item.运装卸费.ToString("0.00##");
                    _paidAmountText.Text = item.已收付货款.ToString("0.00##");
                    _payeeText.Text = string.IsNullOrEmpty(item.收付款人) ? "---" : item.收付款人;
                    _clientText.Text = item.客户供应商;
                    _transferText.Text = item.运输方式;
                    _noteText.Text = item.备注;
                }
            }

            private void InitializeControls()
            {
                _typeLabel.Text = "类型：";
                _dateLabel.Text = "日期：";
                _totalLabel.Text = "合计：";
                _productLabel.Text = "货物：";
                _unitLabel.Text = "单位：";
                _amountLabel.Text = "数量：";
                _priceLabel.Text = "单价：";
                _loadingFeeLabel.Text = "运费：";
                _paidAmountLabel.Text = "已收款";
                _payeeLabel.Text = "收款人：";
                _clientLabel.Text = "客户：";
                _transferLabel.Text = "运输：";
                _noteLabel.Text = "备注：";

                // row 1
                this.Controls.Add(_dateLabel);
                this.Controls.Add(_dateText);

                this.Controls.Add(_productLabel);
                this.Controls.Add(_productText);

                this.Controls.Add(_unitLabel);
                this.Controls.Add(_unitText);

                this.Controls.Add(_paidAmountLabel);
                this.Controls.Add(_paidAmountText);

                // row 2
                this.Controls.Add(_typeLabel);
                this.Controls.Add(_typeText);

                this.Controls.Add(_amountLabel);
                this.Controls.Add(_amountText);

                this.Controls.Add(_transferLabel);
                this.Controls.Add(_transferText);

                this.Controls.Add(_payeeLabel);
                this.Controls.Add(_payeeText);

                // row 3
                this.Controls.Add(_clientLabel);
                this.Controls.Add(_clientText);

                this.Controls.Add(_priceLabel);
                this.Controls.Add(_priceText);

                this.Controls.Add(_loadingFeeLabel);
                this.Controls.Add(_loadingFeeText);

                this.Controls.Add(_totalLabel);
                this.Controls.Add(_totalText);


                int left = 0, top = 0;

                //ctrl index: 8,1,2,3,4,5,6,7
                int[] txtWidthDefs = { 100, 50, 130, 50, 200, 50, 120, 70 };
                int max_X = 0;

                for (int i = 0; i < this.Controls.Count; i++)
                {
                    var widthDefIndex = (i + 1) % 8; // 取余  

                    var ctrl = this.Controls[i];
                    ctrl.Left = left;
                    ctrl.Top = top;
                    ctrl.Width = txtWidthDefs[widthDefIndex];

                    if (widthDefIndex == 0)
                    {
                        max_X = ctrl.Left + ctrl.Width;
                        left = 0;
                        top += (ctrl.Height + 5);
                    }
                    else
                    {
                        left += ctrl.Width;
                    }

                    if (i % 2 == 1)
                    {
                        ctrl.ForeColor = ValueTextColor;
                        ctrl.Font = ValueTextFont;
                    }
                }

                _noteLabel.Left = max_X + 10;
                _noteLabel.Top = 0;

                _noteText.Left = max_X + 10;
                _noteText.Top = _noteLabel.Height + 5;
                _noteText.Width *= 2;
                _noteText.Height *= 2;
                _noteText.ForeColor = ValueTextColor;
                _noteText.Font = ValueTextFont;

                this.Controls.Add(_noteLabel);
                this.Controls.Add(_noteText);
            }
        }
    }
}
